package com.sk.ccflower.server.profile.xmlrpc;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.log4j.Logger;

import com.sk.ccflower.lib.commonBeans.XmlRpcResultBean;
import com.sk.ccflower.lib.commonUtils.FileUtil;
import com.sk.ccflower.lib.imgUtils.ThumbnailUtil;
import com.sk.ccflower.lib.pfBeans.DefaultPhotoBean;
import com.sk.ccflower.lib.pfBeans.ExtPhotoBean;
import com.sk.ccflower.server.common.xmlRpcManager.BaseXmlRpcWorker;
import com.sk.ccflower.server.lib.dbUtils.ConnectionPoolManager;
import com.sk.ccflower.server.lib.sysUtils.Environment;

public class PhotoXmlRpc extends BaseXmlRpcWorker {
	
	private Logger m_log;
	
	public PhotoXmlRpc(ConnectionPoolManager dbcp) {
		super(dbcp);
		this.m_log = Logger.getLogger( getClass() );
		
		this.m_log.debug( "PhotoXmlRpc Constructor called!" );
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		
		this.m_log.debug( "PhotoXmlRpc finalize() called!" );
	}
	
	public XmlRpcResultBean saveDefaultPhotoWork( DefaultPhotoBean bean ) throws Exception {
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		
		try {
			String path = null;
			File file = null;
			FileOutputStream fos = null;
			
			if ( bean.email == null ) {
				retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
				retBean.resultMsg = "이메일 주소에 문제가 있습니다.";
				this.m_log.error( retBean.resultMsg );
				return retBean;
			}
			
			path = Environment.getCcFlowerHome()+"/data/profile";
			path += "/" + bean.email;
			FileUtil fu = new FileUtil();
			fu.newDir( path );
			
			// 눈 사진 저장
			if ( bean.pic_e == null ) {
				retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
				retBean.resultMsg = "눈 사진 데이터에 문제가 있습니다.";
				this.m_log.error( retBean.resultMsg );
				return retBean;
			}
			path = Environment.getCcFlowerHome()+"/data/profile";
			path += "/" + bean.email;
			path += "/eye.jpg";
			file = new File( path );
			fos = new FileOutputStream( file );
			fos.write( bean.pic_e );
			fos.flush();
			fos.close();
			
			// 코 사진 저장
			if ( bean.pic_n == null ) {
				retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
				retBean.resultMsg = "코 사진 데이터에 문제가 있습니다.";
				this.m_log.error( retBean.resultMsg );
				return retBean;
			}
			path = Environment.getCcFlowerHome()+"/data/profile";
			path += "/" + bean.email;
			path += "/nose.jpg";
			file = new File( path );
			fos = new FileOutputStream( file );
			fos.write( bean.pic_n );
			fos.flush();
			fos.close();
			
			// 입 사진 저장
			if ( bean.pic_m == null ) {
				retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
				retBean.resultMsg = "입 사진 데이터에 문제가 있습니다.";
				this.m_log.error( retBean.resultMsg );
				return retBean;
			}
			path = Environment.getCcFlowerHome()+"/data/profile";
			path += "/" + bean.email;
			path += "/mouth.jpg";
			file = new File( path );
			fos = new FileOutputStream( file );
			fos.write( bean.pic_m );
			fos.flush();
			fos.close();
			
			// 얼굴 사진 저장
			if ( bean.pic_f == null ) {
				retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
				retBean.resultMsg = "얼굴 사진 데이터에 문제가 있습니다.";
				this.m_log.error( retBean.resultMsg );
				return retBean;
			}
			path = Environment.getCcFlowerHome()+"/data/profile";
			path += "/" + bean.email;
			path += "/face.jpg";
			file = new File( path );
			fos = new FileOutputStream( file );
			fos.write( bean.pic_f );
			fos.flush();
			fos.close();
			
			// 얼굴사진 썸네일 생성
			String src = path;
			String dest = Environment.getCcFlowerHome()+"/data/profile";
			dest += "/" + bean.email;
			dest += "/face_t.jpg";
			ThumbnailUtil.thumbnailImage_ImageMagick( src, dest, 110, 90 );
			
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_SUCCESS;
			retBean.resultMsg = "사진 데이터를 모두 저장 했습니다.";
			
			return retBean;
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE : " + e.getMessage();
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			
		}
	}
	
	public XmlRpcResultBean saveExtPhotoWork( ExtPhotoBean bean ) throws Exception {
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		
		try {
			String path = null;
			File file = null;
			FileOutputStream fos = null;
			
			if ( bean.email == null ) {
				retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
				retBean.resultMsg = "이메일 주소에 문제가 있습니다.";
				this.m_log.error( retBean.resultMsg );
				return retBean;
			}
			
			path = Environment.getCcFlowerHome()+"/data/profile";
			path += "/" + bean.email;
			FileUtil fu = new FileUtil();
			fu.newDir( path );
			
			// 사진1 저장
			if ( bean.pic_add1 != null ) {
				path = Environment.getCcFlowerHome()+"/data/profile";
				path += "/" + bean.email;
				path += "/add1.jpg";
				file = new File( path );
				fos = new FileOutputStream( file );
				fos.write( bean.pic_add1 );
				fos.flush();
				fos.close();
				
				// 썸네일 생성
				String src = path;
				String dest = Environment.getCcFlowerHome()+"/data/profile";
				dest += "/" + bean.email;
				dest += "/add1_t.jpg";
				ThumbnailUtil.thumbnailImage_ImageMagick( src, dest, 110, 90 );
			}
			
			// 사진2 저장
			if ( bean.pic_add2 != null ) {
				path = Environment.getCcFlowerHome()+"/data/profile";
				path += "/" + bean.email;
				path += "/add2.jpg";
				file = new File( path );
				fos = new FileOutputStream( file );
				fos.write( bean.pic_add2 );
				fos.flush();
				fos.close();
				
				// 썸네일 생성
				String src = path;
				String dest = Environment.getCcFlowerHome()+"/data/profile";
				dest += "/" + bean.email;
				dest += "/add2_t.jpg";
				ThumbnailUtil.thumbnailImage_ImageMagick( src, dest, 110, 90 );
			}
			
			// 사진3 저장
			if ( bean.pic_add3 != null ) {
				path = Environment.getCcFlowerHome()+"/data/profile";
				path += "/" + bean.email;
				path += "/add3.jpg";
				file = new File( path );
				fos = new FileOutputStream( file );
				fos.write( bean.pic_add3 );
				fos.flush();
				fos.close();
				
				// 썸네일 생성
				String src = path;
				String dest = Environment.getCcFlowerHome()+"/data/profile";
				dest += "/" + bean.email;
				dest += "/add3_t.jpg";
				ThumbnailUtil.thumbnailImage_ImageMagick( src, dest, 110, 90 );
			}
			
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_SUCCESS;
			retBean.resultMsg = "사진 데이터를 모두 저장 했습니다.";
			
			return retBean;
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE : " + e.getMessage();
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			
		}
	}
	
}
